<?php

namespace app\api\module;

use app\common\model\Rental as RentalModel;
use app\common\model\RentalCategory;
use app\common\model\RentalSearch;
use think\facade\Db;

class Rental extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_RENTAL_TOP,
            ZFN_RENTAL_SEARCH_DATA,
            ZFN_RENTAL_CATEGORY,
            ZFN_RENTAL_SEARCH_LIST,
            ZFN_RENTAL_COMMUNITY_SEARCH_DATA,
            ZFN_RENTAL_COMMUNITY_RENTAL_LIST,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {
            //【1001】租房首页顶部
            case ZFN_RENTAL_TOP:
                return $this->rentalTop();
            //【1002】租房筛选项数据
            case ZFN_RENTAL_SEARCH_DATA:
                return $this->rentalSearchData();
            //【1003】租房分类
            case ZFN_RENTAL_CATEGORY:
                return $this->rentalCategory();
            //【1004】租房筛选列表
            case ZFN_RENTAL_SEARCH_LIST:
                return $this->rentalSearchList();
            //【1005】小区下租房筛选项
            case ZFN_RENTAL_COMMUNITY_SEARCH_DATA:
                return $this->communitySearchData();
            //【1006】小区下租房列表
            case ZFN_RENTAL_COMMUNITY_RENTAL_LIST:
                return $this->communityRentalList();
            //【1007】租房详情
            case ZFN_RENTAL_DETAIL:
                return $this->rentalDetail();
            //【1008】出租房分享
            case ZFN_RENTAL_SHARE:
                return $this->rentalShare();
            default:
                send_developer_error("模块【租房】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function rentalTop()
    {
        $data = RentalModel::indexTop();
        $this->insertResponse('',$data);
        return $this->response('租房顶部信息');
    }

    private function rentalSearchData()
    {
        $city_id = $this->essentialParam('city_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $data = RentalSearch::searchData($city_id,$longitude,$latitude);
        $this->insertResponse('',$data);
        return $this->response('租房筛选项数据');
    }

    private function rentalCategory()
    {
        $data = RentalCategory::getList();
        $this->insertResponse('data',$data);
        return $this->response('租房分类');
    }

    private function rentalSearchList()
    {
        $city_id = $this->essentialParam('city_id');
        $category_id = $this->optionalParam('category_id');
        $keyword = $this->optionalParam('keyword');
        $area_id = $this->optionalParam('area_id');
        $district_ids = $this->optionalParam('district_ids');
        $nearby = $this->optionalParam('nearby');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $sel_data = [
            'rental_type' => $this->optionalParam('rental_type'),
            'bedroom' => $this->optionalParam('bedroom'),
            'house_tags' => $this->optionalParam('house_tags'),
            'house_direct' => $this->optionalParam('house_direct'),
            'build_area' => $this->optionalParam('build_area'),

            'price_min' => $this->optionalParam('price_min'),
            'price_max' => $this->optionalParam('price_max'),

            'floor_tag' => $this->optionalParam('floor_tag'),
            'facility' => $this->optionalParam('facility'),
            'light' => $this->optionalParam('light'),
            'check_in' => $this->optionalParam('check_in'),
        ];
        $sort = $this->optionalParam('sort');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = RentalModel::searchList($city_id,$category_id,$keyword,$area_id,$district_ids,$nearby,$longitude,$latitude,$sel_data,$sort,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('租房列表');
    }

    private function communitySearchData()
    {
        $community_id = $this->essentialParam('community_id');
        $data = RentalSearch::communitySearchData($community_id);
        $this->insertResponse('',$data);
        return $this->response('小区下的租房筛选项数据');
    }

    private function communityRentalList()
    {
        $community_id = $this->essentialParam('community_id');
        $sel_data = [
            'rental_type' => $this->optionalParam('rental_type'),
            'bedroom' => $this->optionalParam('bedroom'),
            'house_tags' => $this->optionalParam('house_tags'),
            'house_direct' => $this->optionalParam('house_direct'),
            'build_area' => $this->optionalParam('build_area'),

            'price_min' => $this->optionalParam('price_min'),
            'price_max' => $this->optionalParam('price_max'),

            'floor_tag' => $this->optionalParam('floor_tag'),
            'facility' => $this->optionalParam('facility'),
            'light' => $this->optionalParam('light'),
            'check_in' => $this->optionalParam('check_in'),
        ];
        $sort = $this->optionalParam('sort');
        $page = $this->essentialParam('page');
        $page_size = $this->essentialParam('page_size');
        $data = RentalModel::communityRental($community_id,$sel_data,$sort,$page,$page_size);
        $this->insertResponse('data',$data);
        return $this->response('小区下租房列表');
    }

    private function rentalDetail(){
        $user = $this->currentUser();
        $rental_id = $this->essentialParam('rental_id');
        $data = RentalModel::detail($rental_id,$user);
        $this->insertResponse('',$data);
        return $this->response('租房详情');
    }

    private function rentalShare(){
        $user = $this->currentUser(true);
        $rental_id = $this->essentialParam('rental_id');
        $data = RentalModel::share($user,$rental_id);
        $this->insertResponse('',$data);
        return $this->response('出租房分享');
    }
}
